package com.atguigu.educms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.ICrmBannerService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 管理员banner管理接口
 * </p>
 *
 * @author AIR
 * @since 2022-10-05
 */
@RestController
@RequestMapping("/educms/bannerAdmin")
//@CrossOrigin
public class BannerAdminController {

    @Autowired
    private ICrmBannerService crmBannerService;

    //分页查询banner
    @GetMapping("pageBanner/{currentPage}/{limit}")
    public R pageBanner(@PathVariable long currentPage,@PathVariable long limit){
        Page<CrmBanner> pageBanner = new Page<>(currentPage,limit);
        crmBannerService.page(pageBanner,null);
        long total = pageBanner.getTotal();
        System.out.println(total);
        return R.ok().data("item",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    //添加banner
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    //根据id查询banner
    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable String id){
        CrmBanner crmBanner = crmBannerService.getById(id);
        return R.ok().data("item",crmBanner);
    }

    //修改banner
    @CacheEvict(value = "banner",allEntries = true)
    @PutMapping("updateBanner/{id}")
    public R updateBanner(@PathVariable String id,@RequestBody CrmBanner crmBanner){
        crmBanner.setId(id);
        crmBannerService.updateById(crmBanner);
        return R.ok();
    }

    //删除banner
    @CacheEvict(value = "banner",allEntries = true)
    @DeleteMapping("deleteBanner/{id}")
    public R deleteBanner(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }
}
